# -*-coding:utf-8-*-
"""

File Create by Author:Backpackerxl
Create File Time:2021/9/22/022 12:21
Program IDE:PyCharm
File Name:backpackerxl.py

"""
import sys
import time as t


class Backpackerxl:
    def __init__(self):
        self.COLOR = {
            "Black": "\033[30m",
            "Red": "\033[31m",
            "Green": "\033[32m",
            "Yello": "\033[33m",
            "Blue": "\033[34m",
            "Purple": "\033[35m",
            "Skyblue": "\033[36m",
            "White": "\033[38m",
        }
        self.Tail = "\033[0m"

    def XlformateNowTime(self, time_stamp, tag):
        '''
        获取当前系统时间
        YY-MM-DD ->年月日
        MM-DD -> 月日
        YY ->年
        YY-MM-DD-WW ->年月日+星期
        WW ->星期
        HH-MM-SS 时分秒
        HH-MM 时分
        '''
        y = time_stamp[0]
        m = time_stamp[1]
        d = time_stamp[2]
        h = time_stamp[3]
        min = time_stamp[4] if time_stamp[4] >= 10 else f'0{time_stamp[4]}'
        sec = time_stamp[5] if time_stamp[5] >= 10 else f'0{time_stamp[5]}'
        if time_stamp[6] == 0:
            t_wed = '周一'
        elif time_stamp[6] == 1:
            t_wed = '周二'
        elif time_stamp[6] == 2:
            t_wed = '周三'
        elif time_stamp[6] == 3:
            t_wed = '周四'
        elif time_stamp[6] == 4:
            t_wed = '周五'
        elif time_stamp[6] == 5:
            t_wed = '周六'
        elif time_stamp[6] == 6:
            t_wed = '周日'

        if tag == 'YY-MM-DD':
            return f'{y}年{m}月{d}日'
        elif tag == 'YY-MM-DD-WW':
            return f'{y}年{m}月{d}日 ,{t_wed}'
        elif tag == 'MM-DD':
            return f'{m}月{d}日'
        elif tag == 'HH-MM-SS':
            return f'{h}:{min}:{sec}'
        elif tag == 'HH-MM':
            return f'{h}:{min}'
        elif tag == 'YY':
            return f'{y}年'
        elif tag == 'WW':
            return f'{t_wed}'
        else:
            print(self.COLOR.get('Red') + 'args error' + self.Tail)

    Xlprint = lambda self, val, color='Skyblue', xlend='': print(f'{self.COLOR.get(color)}{val}{self.Tail}',
                                                                 end=xlend)
    '''
               打印显示的颜色
               :param txt: 提示文本
               :param color:
               文字显示的颜色(数字类型)
               Black ==> 黑色
               Red ==> 红色
               Green ==> 绿色
               Yello ==> 黄色
               Blue ==> 蓝色
               Purple ==> 紫色
               Skyblue ==> 天蓝色
               White ==> 白色
               :return: 返回print

               '''
    Xlinput = lambda self, val="请输入：", color='Skyblue': input(self.COLOR.get(color) + val + self.Tail)
    '''
                   输入显示的颜色
                   :param txt: 提示文本
                   :param color:
                   文字显示的颜色(数字类型)
                   Black ==> 黑色
                   Red ==> 红色
                   Green ==> 绿色
                   Yello ==> 黄色
                   Blue ==> 蓝色
                   Purple ==> 紫色
                   Skyblue ==> 天蓝色
                   White ==> 白色
                   :return: 返回input

                   '''

    def XlTypewriter(self, val, color='Skyblue', s=0.5):
        '''
        打字机效果
        :param i: 打印字符
        :param count: 控制换行
        :param val: 控制每一行打印的个数
        :param colornu:
        文字显示的颜色(数字类型)
                   Black ==> 黑色
                   Red ==> 红色
                   Green ==> 绿色
                   Yello ==> 黄色
                   Blue ==> 蓝色
                   Purple ==> 紫色
                   Skyblue ==> 天蓝色
                   White ==> 白色
        :param speed: 控制打印速度(s)
        '''

        for i in val:
            print(self.COLOR.get(color) + i + self.Tail, end=''),
            sys.stdout.flush()
            t.sleep(s)
        return val

    def isPrimeNumbers(self, val):
        '''
        判断是否为素数
        :param val: 待判断的数
        :return: 返回判断值
        '''
        flag = False
        for i in range(2, val):
            if val % i == 0:
                flag = True
                break
        return flag

    def XlSortlist(self, arr, tag='ASC'):
        '''
        :arr 一个可排序的对象
        :tag 代表升序还是降序
        ASC ->代表升序
        DESC ->代表降序
        默认升序
        '''

        def changArray(arr, j):
            '''
            内部函数
            交换
            '''
            arr[j + 1] = arr[j + 1] ^ arr[j]
            arr[j] = arr[j + 1] ^ arr[j]
            arr[j + 1] = arr[j + 1] ^ arr[j]

        lens = len(arr)
        for i in range(lens):
            for j in range(0, lens - 1):
                if tag == 'ASC':
                    if arr[j + 1] < arr[j]:
                        changArray(arr, j)
                elif tag == 'DESC':
                    if arr[j + 1] > arr[j]:
                        changArray(arr, j)
                else:
                    print(self.COLOR.get('Red') + 'tag is error' + self.Tail)
        return arr

    def Xldrmlist(self, arr):
        '''
        去掉列表中重复的数据
        :arr -> 一个带有重复数据的列表
        '''
        temp = []
        for i in arr:
            temp.append(i) if i not in temp else temp
        return temp

    # XlFactorial = lambda self, num: num if num == 1 else num * self.XlFactorial(num - 1)
    def XlFactorial(self, num):
        '''
        计算阶乘
        :num 一个数
        '''
        res = 1
        if isinstance(num, int):
            for i in range(1, num + 1):
                res *= i
            return res
        else:
            print(self.COLOR.get('Red') + 'Is not int' + self.Tail)

    def XlcountLetter(self, str_letter):
        '''
        统计字符串中的重复字母的个数
        :str_letter -> 没有空格的字符串
        '''
        str_letter_d = {}
        for i in str_letter:
            if i not in str_letter_d:
                str_letter_d[i] = 0

            for j in str_letter_d:
                if i == j:
                    str_letter_d[i] += 1
                    break

        return str_letter_d

    def XlcountWord(self, str_word):
        '''
        统计一个句子中的单词重复个数结尾必须有空格
        :str_word -> 一个句子
        '''
        word_count = {}
        words = ''
        for i in str_word:
            if i != ' ':
                words += i
            else:
                if words not in word_count:
                    word_count[words] = 0
                for tag in word_count:
                    if words == tag:
                        word_count[tag] += 1
                        break
                words = ''
                continue
        return word_count

    def XlDownloadStatus(self, total, color='Skyblue'):
        """
        下载动画
        :total -> 下载总量
        """
        print(self.COLOR.get(color) + '=' * 26 + ' #正在下载# ' + '=' * 25 + self.Tail)
        for i in range(0, total + 1):
            completed = '#' * i
            incompleted = '.' * (total - i)
            percentage = i / total * 100
            print(f'\r {self.COLOR.get(color)}{round(percentage, 1)}%[{completed}{incompleted}]{self.Tail}', end='')
            t.sleep(0.5)
        print('\n' + self.COLOR.get(color) + '=' * 26 + ' #下载完成# ' + '=' * 25 + self.Tail)

    def isCallBackNum(self, num):
        '''
        判断一个数是不是回文数
        '''
        cnum = num
        call = ''
        while cnum != 0:
            call += str(cnum % 10)
            cnum /= 10
            cnum = int(cnum)

        info = f'{num}是回文数' if int(call) == num else f'{num}不是回文数'
        return info

    def Xlmultiple(self, *args):
        '''
        辗转相除法，求两个数的最大公约数
        返回最大公约数
        '''
        x = args[0]
        y = args[1]
        while y != 0:
            if x < y:
                x = x ^ y
                y = x ^ y
                x = x ^ y
            t = x % y
            x = y
            y = t

        return x

    def XlMergelist(self, arr, tag='ASC'):
        '''
        归并排序算法
        返回一个排序后的列表
        ASC ->代表升序
        DESC ->代表降序
        默认升序
        '''
        # 拆分部分
        n = len(arr)
        if n <= 1:
            return arr
        mid = n // 2
        l_arr = self.XlMergelist(arr[:mid], tag)
        r_arr = self.XlMergelist(arr[mid:], tag)
        # 归并部分
        res = []
        l_pop = r_pop = 0
        l_len = len(l_arr)
        r_len = len(r_arr)
        while l_pop < l_len and r_pop < r_len:
            if l_arr[l_pop] < r_arr[r_pop] if tag == 'ASC' else l_arr[l_pop] > r_arr[r_pop]:
                res.append(l_arr[l_pop])
                l_pop += 1
            else:
                res.append(r_arr[r_pop])
                r_pop += 1
        res.extend(r_arr[r_pop:]) if l_pop == l_len else res.extend(l_arr[l_pop:])

        return res


class xlUtil(Backpackerxl):
    pass
